//
//  CellOfHot_area.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/8.
//  Copyright © 2016年 KX. All rights reserved.
#import "CellOfHot_areaHead.h"
#import "CellOfCellOfHot_area.h"
#import "CellOfHot_area.h"
#import "WebViewOfCommendViewController.h"
#import "FreeTravelMacro.h"
#import "TravelShopViewController.h"
@interface CellOfHot_area ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation CellOfHot_area
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCollectionView];
    }
    return self;
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    self.collectionView.scrollEnabled = NO;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH-20, 100);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[CellOfCellOfHot_area class] forCellWithReuseIdentifier:@"CellOfCellOfHot_area"];
    [self.collectionView registerClass:[CellOfHot_areaHead class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CellOfHot_areaHead"];
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WebViewOfCommendViewController *vc = [[WebViewOfCommendViewController alloc]init];
    ModelOfHot_areaList *model = self.model.list[indexPath.row];
    vc.url = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=7.0.1&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6",model.ID];
    TravelShopViewController *travel = (TravelShopViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
    [travel.navigationController pushViewController:vc animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.list.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CellOfCellOfHot_area *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfCellOfHot_area" forIndexPath:indexPath];
    cell.model = self.model.list[indexPath.row];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    CellOfHot_areaHead *head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CellOfHot_areaHead" forIndexPath:indexPath];
        head.model = self.model;
    return head;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH, (SCREEN_WIDTH-50)/4+50);
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
}
- (void)setModel:(ModelOfHot_area *)model{
    _model = model;
    
    [self.collectionView reloadData];
}
@end
